/*----------------------------------------------------------------------
  - File name      : STC8Ax_PCA.c
  - Author         : Quark Team
  - Update date    : 2020-12-05                   
  -	Copyright      : Gevico Electronic studio   
  - Module comments: PCA module.
-----------------------------------------------------------------------*/
/*-----------------------------------------------------------------------
|                               INCLUDES                                |
-----------------------------------------------------------------------*/
#include "STC8Ax_PCA.h"
/*-----------------------------------------------------------------------
|                                 DATA                                  |
-----------------------------------------------------------------------*/
extern uint32 SYSCLK_FRE;
static uint16 PCA_Value[4];
static uint16 PCA_Rvalue[4];
/*-----------------------------------------------------------------------
|                               FUNCTION                                |
-----------------------------------------------------------------------*/

/*--------------------------------------------------------
| @Description: PCA0 counter init function               |
| @param      : None                                     |
| @return     : FSCSTATE : Success / erro                |
--------------------------------------------------------*/

FSCSTATE PCA_CNT_Init(PCACLKSrc_Type CLKSrc,uint16 Value,FUNSTATE Run) 
{
    CMOD = (CMOD & 0xF1)|(CLKSrc);
    CL = Value;
    CH = Value >> 8;
    CR = Run;
    return FSC_SUCCESS;
}

/*--------------------------------------------------------
| @Description: PCA0 PWM init function                   |
| @param      : PCA_PWMBits_Type PWMBit,uint16 Duty      |
| @return     : FSCSTATE : Success / erro                |
--------------------------------------------------------*/

FSCSTATE PCA0_PWM_Init(PCA_PWMBits_Type PWMBits,uint16 Duty) 
{
    CCAPM0 = 0x42;
    PCA_PWM0 = (PCA_PWM0 & 0x3F)|(PWMBits << 6);
		if(PWMBits == PCA_PWM_6Bit)
		{
			PCA_PWM0 = (PCA_PWM0 & 0XFE) | ((Duty & 0x40) >> 6); //EPC0L
			PCA_PWM0 = (PCA_PWM0 & 0XFD) | ((Duty & 0x40) >> 5); //EPC0H			
			CCAP0L = Duty & 0xC0;
			CCAP0H = Duty & 0xC0;		 
		}
		else if(PWMBits == PCA_PWM_7Bit)
		{
			PCA_PWM0 = (PCA_PWM0 & 0XFE) | ((Duty & 0x80) >> 7); //EPC0L
			PCA_PWM0 = (PCA_PWM0 & 0XFD) | ((Duty & 0x80) >> 6); //EPC0H			
			CCAP0L = Duty & 0x80;
			CCAP0H = Duty & 0x80;		 		
		}
		else if(PWMBits == PCA_PWM_8Bit)
		{	
			PCA_PWM0 = (PCA_PWM0 & 0XFE) | ((Duty & 0x100) >> 8); //EPC0L
			PCA_PWM0 = (PCA_PWM0 & 0XFD) | ((Duty & 0x100) >> 7); //EPC0H			
			CCAP0L = Duty;
			CCAP0H = Duty;		 					
		}
		else if(PWMBits == PCA_PWM_10Bit)
		{
			PCA_PWM0 = (PCA_PWM0 & 0XFE) | ((Duty & 0x400) >> 10); //EPC0L
			PCA_PWM0 = (PCA_PWM0 & 0XFD) | ((Duty & 0x400) >> 9); //EPC0H					
			PCA_PWM0 = (PCA_PWM0 & 0XF3) | ((Duty & 0x300) >> 6); //XCCAP0L
			PCA_PWM0 = (PCA_PWM0 & 0XCF) | ((Duty & 0x300) >> 4); //XCCAP0H	
			CCAP0L = Duty;
			CCAP0H = Duty;		 					
		}
		else  return FSC_FAIL;
			
    return FSC_SUCCESS;
}

/*--------------------------------------------------------
| @Description: PCA1 PWM init function                   |
| @param      : PCA_PWMBits_Type PWMBit,uint16 Duty      |
| @return     : FSCSTATE : Success / erro                |
--------------------------------------------------------*/

FSCSTATE PCA1_PWM_Init(PCA_PWMBits_Type PWMBits,uint16 Duty) 
{
    CCAPM1 = 0x42;
    PCA_PWM1 = (PCA_PWM1 & 0x3F)|(PWMBits << 6);
		if(PWMBits == PCA_PWM_6Bit)
		{
			PCA_PWM1 = (PCA_PWM1 & 0XFE) | ((Duty & 0x40) >> 6); //EPC0L
			PCA_PWM1 = (PCA_PWM1 & 0XFD) | ((Duty & 0x40) >> 5); //EPC0H			
			CCAP1L = Duty & (0xC0 | (PCA_PWM_6Bit) );
			CCAP1H = Duty & 0xC0;		 
		}
		else if(PWMBits == PCA_PWM_7Bit)
		{
			PCA_PWM1 = (PCA_PWM1 & 0XFE) | ((Duty & 0x80) >> 7); //EPC0L
			PCA_PWM1 = (PCA_PWM1 & 0XFD) | ((Duty & 0x80) >> 6); //EPC0H			
			CCAP1L = Duty & 0x80;
			CCAP1H = Duty & 0x80;
		}
		else if(PWMBits == PCA_PWM_8Bit)
		{	
			PCA_PWM1 = (PCA_PWM1 & 0XFE) | ((Duty & 0x100) >> 8); //EPC0L
			PCA_PWM1 = (PCA_PWM1 & 0XFD) | ((Duty & 0x100) >> 7); //EPC0H			
			CCAP1L = Duty;
			CCAP1H = Duty;
		}
		else if(PWMBits == PCA_PWM_10Bit)
		{
			PCA_PWM1 = (PCA_PWM1 & 0XFE) | ((Duty & 0x400) >> 10); //EPC0L
			PCA_PWM1 = (PCA_PWM1 & 0XFD) | ((Duty & 0x400) >> 9); //EPC0H					
			PCA_PWM1 = (PCA_PWM1 & 0XF3) | ((Duty & 0x300) >> 6); //XCCAP0L
			PCA_PWM1 = (PCA_PWM1 & 0XCF) | ((Duty & 0x300) >> 4); //XCCAP0H	
			CCAP1L = Duty;
			CCAP1H = Duty;		 					
		}
		else  return FSC_FAIL;
			
    return FSC_SUCCESS;
}

/*--------------------------------------------------------
| @Description: PCA2 PWM init function                   |
| @param      : PCA_PWMBits_Type PWMBits,uint16 Duty     |
| @return     : FSCSTATE : Success / erro                |
--------------------------------------------------------*/

FSCSTATE PCA2_PWM_Init(PCA_PWMBits_Type PWMBits,uint16 Duty) 
{
    CCAPM2 = 0x42;
    PCA_PWM2 = (PCA_PWM2 & 0x3F)|(PWMBits << 6);
		if(PWMBits == PCA_PWM_6Bit)
		{
			PCA_PWM2 = (PCA_PWM2 & 0XFE) | ((Duty & 0x40) >> 6); //EPC0L
			PCA_PWM2 = (PCA_PWM2 & 0XFD) | ((Duty & 0x40) >> 5); //EPC0H			
			CCAP2L = Duty & 0xC0;
			CCAP2H = Duty & 0xC0;		 
		}
		else if(PWMBits == PCA_PWM_7Bit)
		{
			PCA_PWM2 = (PCA_PWM2 & 0XFE) | ((Duty & 0x80) >> 7); //EPC0L
			PCA_PWM2 = (PCA_PWM2 & 0XFD) | ((Duty & 0x80) >> 6); //EPC0H			
			CCAP2L = Duty & 0x80;
			CCAP2H = Duty & 0x80;		 		
		}
		else if(PWMBits == PCA_PWM_8Bit)
		{	
			PCA_PWM2 = (PCA_PWM2 & 0XFE) | ((Duty & 0x100) >> 8); //EPC0L
			PCA_PWM2 = (PCA_PWM2 & 0XFD) | ((Duty & 0x100) >> 7); //EPC0H			
			CCAP2L = Duty;
			CCAP2H = Duty;		 					
		}
		else if(PWMBits == PCA_PWM_10Bit)
		{
			PCA_PWM2 = (PCA_PWM2 & 0XFE) | ((Duty & 0x400) >> 10); //EPC0L
			PCA_PWM2 = (PCA_PWM2 & 0XFD) | ((Duty & 0x400) >> 9); //EPC0H					
			PCA_PWM2 = (PCA_PWM2 & 0XF3) | ((Duty & 0x300) >> 6); //XCCAP0L
			PCA_PWM2 = (PCA_PWM2 & 0XCF) | ((Duty & 0x300) >> 4); //XCCAP0H	
			CCAP2L = Duty;
			CCAP2H = Duty;		 					
		}
		else  return FSC_FAIL;
			
    return FSC_SUCCESS;
}

/*--------------------------------------------------------
| @Description: PCA3 PWM init function                   |
| @param      : PCA_PWMBits_Type PWMBits,uint16 Duty     |
| @return     : FSCSTATE : Success / erro                |
--------------------------------------------------------*/

FSCSTATE PCA3_PWM_Init(PCA_PWMBits_Type PWMBits,uint16 Duty) 
{
    CCAPM3 = 0x42;
    PCA_PWM3 = (PCA_PWM3 & 0x3F)|(PWMBits << 6);
		if(PWMBits == PCA_PWM_6Bit)
		{
			PCA_PWM3 = (PCA_PWM3 & 0XFE) | ((Duty & 0x40) >> 6); //EPC0L
			PCA_PWM3 = (PCA_PWM3 & 0XFD) | ((Duty & 0x40) >> 5); //EPC0H			
			CCAP3L = Duty & (0xC0 | (PCA_PWM_6Bit) );
			CCAP3H = Duty & 0xC0;		 
		}
		else if(PWMBits == PCA_PWM_7Bit)
		{
			PCA_PWM3 = (PCA_PWM3 & 0XFE) | ((Duty & 0x80) >> 7); //EPC0L
			PCA_PWM3 = (PCA_PWM3 & 0XFD) | ((Duty & 0x80) >> 6); //EPC0H			
			CCAP3L = Duty & 0x80;
			CCAP3H = Duty & 0x80;
		}
		else if(PWMBits == PCA_PWM_8Bit)
		{	
			PCA_PWM3 = (PCA_PWM3 & 0XFE) | ((Duty & 0x100) >> 8); //EPC0L
			PCA_PWM3 = (PCA_PWM3 & 0XFD) | ((Duty & 0x100) >> 7); //EPC0H			
			CCAP3L = Duty;
			CCAP3H = Duty;
		}
		else if(PWMBits == PCA_PWM_10Bit)
		{
			PCA_PWM3 = (PCA_PWM3 & 0XFE) | ((Duty & 0x400) >> 10); //EPC0L
			PCA_PWM3 = (PCA_PWM3 & 0XFD) | ((Duty & 0x400) >> 9); //EPC0H					
			PCA_PWM3 = (PCA_PWM3 & 0XF3) | ((Duty & 0x300) >> 6); //XCCAP0L
			PCA_PWM3 = (PCA_PWM3 & 0XCF) | ((Duty & 0x300) >> 4); //XCCAP0H	
			CCAP3L = Duty;
			CCAP3H = Duty;		 					
		}
		else  return FSC_FAIL;
			
    return FSC_SUCCESS;
}

/*--------------------------------------------------------
| @Description: PCA0 TIM init function                   |
| @param      : uint16 Value                             |
| @return     : FSCSTATE : Success / erro                |
--------------------------------------------------------*/

FSCSTATE PCA0_TIM_Init(uint16 Value) 
{
    CCAPM0 = 0x48;
    Value = SYSCLK_FRE / (12000000UL / Value);
    PCA_Value[0] = Value;
    CCAP0L = Value;
    CCAP0H = Value >> 8;
    return FSC_SUCCESS;
}

/*--------------------------------------------------------
| @Description: PCA1 TIM init function                   |
| @param      : uint16 Value                             |
| @return     : FSCSTATE : Success / erro                |
--------------------------------------------------------*/

FSCSTATE PCA1_TIM_Init(uint16 Value) 
{
    CCAPM1 = 0x48;
    Value = SYSCLK_FRE / (12000000UL / Value);
    PCA_Value[1] = Value;
    CCAP1L = Value;
    CCAP1H = Value >> 8;
    return FSC_SUCCESS;
}

/*--------------------------------------------------------
| @Description: PCA2 TIM init function                   |
| @param      : uint16 Value                             |
| @return     : FSCSTATE : Success / erro                |
--------------------------------------------------------*/

FSCSTATE PCA2_TIM_Init(uint16 Value) 
{
    CCAPM2 = 0x48;
    Value = SYSCLK_FRE / (12000000UL / Value);
    PCA_Value[2] = Value;
    CCAP2L = Value;
    CCAP2H = Value >> 8;
    return FSC_SUCCESS;
}

/*--------------------------------------------------------
| @Description: PCA3 TIM init function                   |
| @param      : uint16 Value                             |
| @return     : FSCSTATE : Success / erro                |
--------------------------------------------------------*/

FSCSTATE PCA3_TIM_Init(uint16 Value) 
{
    CCAPM3 = 0x48;
    Value = SYSCLK_FRE / (12000000UL / Value);
    PCA_Value[3] = Value;
    CCAP3L = Value;
    CCAP3H = Value >> 8;
    return FSC_SUCCESS;
}

/*--------------------------------------------------------
| @Description: PCA0 POP init function                   |
| @param      : uint16 Value                             |
| @return     : FSCSTATE : Success / erro                |
--------------------------------------------------------*/

FSCSTATE PCA0_POP_Init(uint16 Value) 
{
    CCAPM0 = 0x4D;
    Value = SYSCLK_FRE / (12UL / Value);
    PCA_Value[0] = Value;
    CCAP0L = Value;
    CCAP0H = Value >> 8;
    return FSC_SUCCESS;
}

/*--------------------------------------------------------
| @Description: PCA1 POP init function                   |
| @param      : uint16 Value                             |
| @return     : FSCSTATE : Success / erro                |
--------------------------------------------------------*/

FSCSTATE PCA1_POP_Init(uint16 Value) 
{
    CCAPM1 = 0x4D;
    Value = SYSCLK_FRE / (12UL / Value);
    PCA_Value[1] = Value;
    CCAP1L = Value;
    CCAP1H = Value >> 8;
    return FSC_SUCCESS;
}

/*--------------------------------------------------------
| @Description: PCA2 POP init function                   |
| @param      : uint16 Value                             |
| @return     : FSCSTATE : Success / erro                |
--------------------------------------------------------*/

FSCSTATE PCA2_POP_Init(uint16 Value) 
{
    CCAPM2 = 0x4D;
    Value = SYSCLK_FRE / (12UL / Value);
    PCA_Value[2] = Value;
    CCAP2L = Value;
    CCAP2H = Value >> 8;
    return FSC_SUCCESS;
}

/*--------------------------------------------------------
| @Description: PCA3 POP init function                   |
| @param      : uint16 Value                             |
| @return     : FSCSTATE : Success / erro                |
--------------------------------------------------------*/

FSCSTATE PCA3_POP_Init(uint16 Value) 
{
    CCAPM3 = 0x4D;
    Value = SYSCLK_FRE / (12UL / Value);
    PCA_Value[3] = Value;
    CCAP3L = Value;
    CCAP3H = Value >> 8;
    return FSC_SUCCESS;
}

/*--------------------------------------------------------
| @Description: PCA0 TIM and POP reload value function   |
| @param      : None                                     |
| @return     : FSCSTATE : Success / erro                |
--------------------------------------------------------*/

void PCA0_TIM_POP_ReValue(void)
{
    PCA_Rvalue[0] += PCA_Value[0];
    CCAP0L = PCA_Rvalue[0];
    CCAP0H = PCA_Rvalue[0] >> 8;
}

/*--------------------------------------------------------
| @Description: PCA1 TIM and POP reload value function   |
| @param      : None                                     |
| @return     : FSCSTATE : Success / erro                |
--------------------------------------------------------*/

void PCA1_TIM_POP_ReValue(void)
{
    PCA_Rvalue[1] += PCA_Value[1];
    CCAP1L = PCA_Rvalue[1];
    CCAP1H = PCA_Rvalue[1] >> 8;
}


/*--------------------------------------------------------
| @Description: PCA2 TIM and POP reload value function   |
| @param      : None                                     |
| @return     : FSCSTATE : Success / erro                |
--------------------------------------------------------*/

void PCA2_TIM_POP_ReValue(void)
{
    PCA_Rvalue[2] += PCA_Value[2];
    CCAP2L = PCA_Rvalue[2];
    CCAP2H = PCA_Rvalue[2] >> 8;
}


/*--------------------------------------------------------
| @Description: PCA3 TIM and POP reload value function   |
| @param      : None                                     |
| @return     : FSCSTATE : Success / erro                |
--------------------------------------------------------*/

void PCA3_TIM_POP_ReValue(void)
{
    PCA_Rvalue[3] += PCA_Value[3];
    CCAP3L = PCA_Rvalue[3];
    CCAP3H = PCA_Rvalue[3] >> 8;
}


/*-----------------------------------------------------------------------
|          END OF FLIE        (C) COPYRIGHT Gevico Electronics          | 
-----------------------------------------------------------------------*/



